import { createRouter, createWebHistory } from 'vue-router'
import homeView from '@/views/home/index.vue'
import loginView from '@/views/login/index.vue'
import cradView from '@/views/home/card.vue'
import xrView from '@/views/home/xr.vue'
import yhxxView from '@/views/home/yhxx.vue'
import yhszView from '@/views/home/yhsz.vue'

import NotFound from '@/views/404.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', redirect: '/home/xr' },
  { path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound },
    { path: '/login', name: 'login', component: loginView, },
    {
      path: '/home', name: 'home', component: homeView, children: [
    { path: 'crad', name: 'crad', component: cradView, },
    { path: 'xr', name: 'xr', component: xrView, },
    { path: 'yhxx', name: 'yhxx', component: yhxxView, },
    { path: 'yhsz', name: 'yhsz', component: yhszView, },

      ]
    },

  ],
})
router.beforeEach((to,from,next)=>{
  if(to.fullPath !='/login'){
    let token=localStorage.getItem('token')
    if(!token){
      next('/login')
    }else{
      next()
    }
  }else{
    next()
  }
})
export default router
